package com.bg;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.session.SqlSession;

import com.BaseAction;
import com.Constants;

public class bgAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4169956111835915512L;
	private  String filename;
	private  String fgmc;
	private  List<Bgfg> bgfgs;
	private  int DMid;
	private  List<Jcs> jcslist;
	private  Jcs jcs;
	private File pdfwj;
	private File file;
	private String  wjname;
	private String  wjcode;
	private String  fgids;
	private String  Jcsids;
	
	private String  Algjz;
	private  int Alid;
	
	private  Allist allists;
	private   List<Allist> allist;
	
	private Wtxx wtxx;
	private  List<Wtxx> wtxxs;
	private   List<DhlMenu> dhlMenus;
	private String  Wtname;
	private String  dalist;
	private String  Wtids;
	private String Mname;
	private  List<Jcs> Jcss;
	
	
	
	private  int Wtid;
	private  int Jcsid;
	
	private Zhsq zhsq;
	
	public int getWtid() {
		return Wtid;
	}



	public void setWtid(int wtid) {
		Wtid = wtid;
	}



	public File getPdfwj() {
		return pdfwj;
	}



	public void setPdfwj(File pdfwj) {
		this.pdfwj = pdfwj;
	}



	public File getFile() {
		return file;
	}



	public void setFile(File file) {
		this.file = file;
	}



	public String getWjname() {
		return wjname;
	}



	public void setWjname(String wjname) {
		this.wjname = wjname;
	}



	public String getWjcode() {
		return wjcode;
	}



	public void setWjcode(String wjcode) {
		this.wjcode = wjcode;
	}

	
	public String jcsglmx() {
		
		
		return SUCCESS;
	}
	
	
	
	public void xzfj() {
		String	savepath = request.getRealPath("/") +"file/scfj/";
		savepath = savepath.replaceAll("\\\\", "/");
		
		
		String lj = savepath + "申请表.docx";
		 try {
	            // path是指欲下载的文件的路径。
	            File file = new File(lj);
	            // 取得文件名。
	            String filename = file.getName();
	            // 取得文件的后缀名。
	            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
	           
	            // 以流的形式下载文件。
	            InputStream fis = new BufferedInputStream(new FileInputStream(lj));
	            byte[] buffer = new byte[fis.available()];
	            fis.read(buffer);
	            fis.close();
	            // 清空response
	            response.reset();
	            // 设置response的Header
	            filename = response.encodeURL(new String(filename.getBytes("gb2312"), "iso8859-1"));
	            response.setHeader("Content-disposition", "attachment; filename=\""+ filename + "\"");
	            response.addHeader("Content-Length", "" + file.length());
	            response.setContentType("application/octet-stream");
	            ServletOutputStream outputstream = response.getOutputStream();
	    		outputstream.write(buffer);
	    		outputstream.flush();	
	    		outputstream.close();
	        } catch (IOException ex) {
	            ex.printStackTrace();
	        }
		   
	        return ;
		
	}
	
	public void savesqxx() {
		
		SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			
			if(zhsq!=null) {
				
				
			
			String	savepath = request.getRealPath("/") +"file/sqwj/";
			savepath = savepath.replaceAll("\\\\", "/");
			
			long timeMillis = System.currentTimeMillis();
			wjcode = timeMillis + "";
			String lj = savepath + wjcode+".pdf";
			
			File f = new File(savepath, wjcode+".pdf");
			
			try {
		        
				if(f.exists())
				{
					f.delete();
				} 	
				
				FileUtils.copyFile(zhsq.getFile(), f);
				zhsq.setFilelj(lj);
				bgmmp.savesqxx(zhsq);
				
				
				
			}catch(Exception e) {
				ajaxJson(e.getMessage());
			}
			
			}
		}catch(Exception e) {
			ajaxJson(e.getMessage());
		} finally {
			sqlSession.close();
		}	
		
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	  public void delJcsxx() {
	  	  Map map = new HashMap();
	  		map.put("Jcsids", Jcsids);
	  	  SqlSession sqlSession = getSqlSession();
	  		try {
	  			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
	  			int a = bgmmp.delJcsxx(map);
	  		} finally {
	  			sqlSession.close();
	  		}
	  	   
	  		 ajaxJson("ok");
	  	  
	    }
	
    public String getJcsids() {
		return Jcsids;
	}



	public void setJcsids(String jcsids) {
		Jcsids = jcsids;
	}



	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String jcsnew() {
		if(Jcsid!=0) {
			Map map = new HashMap();
	  		map.put("Jcsid", Jcsid);
			SqlSession sqlSession = getSqlSession();
			try {
				BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
				jcs = bgmmp.cxjcslist(map);
				if(jcs!=null) {
					    String[]  Wtdaids = jcs.getWtdaids().split(",");
					    int Wtdaid =  Integer.parseInt(Wtdaids[0]);
					    map.put("Wtdaid", Wtdaid);
					    wtxx = bgmmp.getWtlistbyWtdaid(map);
					    Wtid = wtxx.getWtid();
				}
			   
			}catch(Exception e) {
				ajaxJson(e.getMessage());
			} finally {
				sqlSession.close();
			}	
			
		}
		
		return SUCCESS;
	}
	
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public void getnextwt() {
    	
    	SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			jcslist = bgmmp.getnextwt(jcs);
		    Jcs	ls_Jcs = jcslist.get(0);
		   	String a = 	ls_Jcs.getWtdaids();
		   	if(jcs.getWtdaids().equals(a)) {
		   		ajaxJson(jcslist,1);
		   	}else {
		      String[] ls_Wtdaid = jcs.getWtdaids().split(",");
		      int cd = ls_Wtdaid.length;
		      String[] ls_Wtdaid_tk = a.split(",");
		      Map map = new HashMap();
		  	  map.put("Wtdaid", Integer.parseInt(ls_Wtdaid_tk[cd]));
		  	  wtxx = bgmmp.getWtlistbyWtdaid(map);
		  	  List<Wtxx> wtxxlist = new ArrayList<Wtxx>();
		  	  wtxxlist.add(wtxx);
		  	  ajaxJson(wtxxlist,2);
		   	}
		   	
		}catch(Exception e) {
			ajaxJson(e.getMessage());
		} finally {
			sqlSession.close();
		}
    	
    }

public void savejcs() {
	 SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			int a = bgmmp.savejcs(jcs);
		    ajaxJson("success");
		}catch(Exception e) {
			ajaxJson(e.getMessage());
		} finally {
			sqlSession.close();
		}
	   
		 
	  
  }
  
  
  
  
  @SuppressWarnings({ "rawtypes", "unchecked" })
  public void delAlxx() {
  	  Map map = new HashMap();
  		map.put("Alid", Alid);
  	  SqlSession sqlSession = getSqlSession();
  		try {
  			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
  			int a = bgmmp.delAlxx(map);
  		} finally {
  			sqlSession.close();
  		}
  	   
  		 ajaxJson("ok");
  	  
    }
  
  
  
  @SuppressWarnings({ "rawtypes", "unchecked" })
  public void getjcsmxdata() {
  	  Map map = new HashMap();
  		map.put("DMid", DMid);
  	  SqlSession sqlSession = getSqlSession();
  		try {
  			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
  			Jcss = bgmmp.getjcsmxdata(map);
  		} finally {
  			sqlSession.close();
  		}
  	   
  		 ajaxJson(Jcss);
  	  
    }
  

  public void getjcsdata() {
  	  SqlSession sqlSession = getSqlSession();
  		try {
  			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
  			dhlMenus = bgmmp.getjcsdata();
  		} finally {
  			sqlSession.close();
  		}
  	   
  		 ajaxJson(dhlMenus);
  	  
    }
  
  
  @SuppressWarnings({ "rawtypes", "unchecked" })
public void delwtxx() {
	  Map map = new HashMap();
		map.put("Wtids", Wtids);
	  SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			int a = bgmmp.delwtxx(map);
		} finally {
			sqlSession.close();
		}
	   
		 ajaxJson("ok");
	  
  }
  
  
  
  
  @SuppressWarnings({ "rawtypes", "unchecked" })
public void editwtdaxx() {
	   
	   
	   SqlSession sqlSession = getSqlSession(false);
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			 bgmmp.editwtdaxx(wtxx);
			 bgmmp.deletewtdaxx(wtxx);
			 String[] Daname = dalist.split(",");
			 for(int a=0;a<Daname.length;a++) {
				 if(Daname[a]!="") {
					 Map map1 = new HashMap();
					 map1.put("Wtid", wtxx.getWtid());
					 map1.put("Daname", Daname[a]);
					 map1.put("Daxh", a+1);
					 bgmmp.savewtxxitem(map1);
				 }
				 
			 }
			 sqlSession.commit();
			 ajaxJson("ok");
		}catch(Exception e) {
			sqlSession.rollback();
			ajaxJson("no");
           e.printStackTrace();
       } finally {
       	
			sqlSession.close();
		}
		
	   
	   
  }
   @SuppressWarnings({ "rawtypes", "unchecked" })
public void savewtxx() {
	   
	   
	   SqlSession sqlSession = getSqlSession(false);
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			 int Wtid = bgmmp.savewtxxbt(wtxx);
			 Wtid = wtxx.getWtid()-1;
			 String[] Daname = dalist.split(",");
			 for(int a=0;a<Daname.length;a++) {
				 if(Daname[a]!="") {
					 Map map1 = new HashMap();
					 map1.put("Wtid", Wtid);
					 map1.put("Daname", Daname[a]);
					 map1.put("Daxh", a+1);
					 bgmmp.savewtxxitem(map1);
				 }
				 
			 }
			 sqlSession.commit();
			 ajaxJson("ok");
		}catch(Exception e) {
			sqlSession.rollback();
			ajaxJson("no");
            e.printStackTrace();
        } finally {
        	
			sqlSession.close();
		}
		
	   
	   
   }
  
  
    public void gettkdata() {
    	
    	SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			wtxxs = bgmmp.gettkdata();
		} finally {
			sqlSession.close();
		}
		 ajaxJson(wtxxs);
    }
  
    public void getwtdadata() {
    	
    	SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			if(wtxx!=null) {
				wtxxs = bgmmp.getwtdadata(wtxx);
			}
			
		} finally {
			sqlSession.close();
		}
		 ajaxJson(wtxxs);
    }
    
	public void editalxx() {
		   
	  	SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			int a = bgmmp.editalxx(allists);
		} finally {
			sqlSession.close();
		}
	   
		 ajaxJson("ok");
   }

	public void savealxx() {
		   
		  	SqlSession sqlSession = getSqlSession();
			try {
				BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
				int a = bgmmp.savealxx(allists);
			} finally {
				sqlSession.close();
			}
		   
			 ajaxJson("ok");
	   }

   @SuppressWarnings({ "rawtypes", "unchecked" })
public void getaldata() {
	   
	   Map map = new HashMap();
		map.put("gjz", Algjz);
		SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			allist = bgmmp.getaldata(map);
		} finally {
			sqlSession.close();
		}
	   
		 ajaxJson(allist);
   }


	public void findwj() {
		 session.put(Constants.PDFWJ, filename);
		 ajaxJson("1");
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void delfgwj() {
		
		Map map = new HashMap();
		map.put("fgids", fgids);
		SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			bgmmp.delfgwjbyids(map);
		} finally {
			sqlSession.close();
		}
		
		ajaxJson("ok");	
	} 
	
	
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void scfgwj() throws Exception {
	String	savepath = request.getRealPath("/") +"file/bgfg/";
	savepath = savepath.replaceAll("\\\\", "/");
	String[]  hzm= wjname.split("\\.");
	long timeMillis = System.currentTimeMillis();
	wjcode = timeMillis + "";
	String lj = savepath + wjcode+"."+hzm[1];
	
	File f = new File(savepath, wjcode+"."+hzm[1]);
	SqlSession sqlSession = getSqlSession();
	try {
        
		if(f.exists())
		{
			f.delete();
		} 	
			
		FileUtils.copyFile(file, f);
		
		
		BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
		Map map = new HashMap();
		map.put("wjlj", lj);
		map.put("fgmc", hzm[0]);
		bgmmp.Bgfgwjlog(map);
	   }catch (IOException e) {

		e.printStackTrace();
		ajaxJson("no");	
		return;

		}
		sqlSession.close();
		
		ajaxJson("ok");	
		
	}
	
	
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String displayjcs() {
		
		Map params = new HashMap();
		params.put("DMid", DMid);
       SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			jcslist = bgmmp.selectjcslist(params);
			if(jcslist.size()>0) {
				String[]  Wtdaids = jcslist.get(0).getWtdaids().split(",");
			    int Wtdaid =  Integer.parseInt(Wtdaids[0]);
			    params.put("Wtdaid", Wtdaid);
			    wtxx = bgmmp.getWtlistbyWtdaid(params);
			    Wtid = wtxx.getWtid();
			}
			
		    
		} finally {
			sqlSession.close();
		}
		
		return SUCCESS;
	}
	
	
	public void  displayPDF(){
		
		filename = (String) session.get(Constants.PDFWJ);
		   try {
			   File file = new File(filename);
	            FileInputStream fileInputStream = new FileInputStream(file);
	            response.setHeader("Content-Disposition", "attachment;fileName=test.pdf");
	            response.setContentType("multipart/form-data");
	            OutputStream outputStream = response.getOutputStream();
	            IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
	            outputStream.close();
	           
	        } catch(Exception e) {
	            e.printStackTrace();
	        }
	    
		   System.gc(); 
	}
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getfgdata() {
		Map params = new HashMap();
		params.put("fgmc", fgmc);
       SqlSession sqlSession = getSqlSession();
		try {
			BGMapper bgmmp=sqlSession.getMapper(BGMapper.class);
			bgfgs = bgmmp.selectbgfgs(params);
		
		} finally {
			sqlSession.close();
		}
		
		ajaxJson(bgfgs);
		
	}

	public String getFgmc() {
		return fgmc;
	}

	public void setFgmc(String fgmc) {
		this.fgmc = fgmc;
	}
	
	
	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}



	public List<Bgfg> getBgfgs() {
		return bgfgs;
	}



	public void setBgfgs(List<Bgfg> bgfgs) {
		this.bgfgs = bgfgs;
	}

	public int getDMid() {
		return DMid;
	}

	public void setDMid(int dMid) {
		DMid = dMid;
	}

	public List<Jcs> getJcslist() {
		return jcslist;
	}

	public void setJcslist(List<Jcs> jcslist) {
		this.jcslist = jcslist;
	}



	public String getFgids() {
		return fgids;
	}



	public void setFgids(String fgids) {
		this.fgids = fgids;
	}



	public Allist getAllists() {
		return allists;
	}



	public void setAllists(Allist allists) {
		this.allists = allists;
	}



	public String getAlgjz() {
		return Algjz;
	}



	public void setAlgjz(String algjz) {
		Algjz = algjz;
	}



	public List<Allist> getAllist() {
		return allist;
	}



	public void setAllist(List<Allist> allist) {
		this.allist = allist;
	}



	public int getAlid() {
		return Alid;
	}



	public void setAlid(int alid) {
		Alid = alid;
	}



	public Wtxx getWtxx() {
		return wtxx;
	}



	public void setWtxx(Wtxx wtxx) {
		this.wtxx = wtxx;
	}



	public List<Wtxx> getWtxxs() {
		return wtxxs;
	}



	public void setWtxxs(List<Wtxx> wtxxs) {
		this.wtxxs = wtxxs;
	}



	public String getWtname() {
		return Wtname;
	}



	public void setWtname(String wtname) {
		Wtname = wtname;
	}



	public String getDalist() {
		return dalist;
	}



	public void setDalist(String dalist) {
		this.dalist = dalist;
	}



	public String getWtids() {
		return Wtids;
	}



	public void setWtids(String wtids) {
		Wtids = wtids;
	}



	public List<DhlMenu> getDhlMenus() {
		return dhlMenus;
	}



	public void setDhlMenus(List<DhlMenu> dhlMenus) {
		this.dhlMenus = dhlMenus;
	}



	public String getMname() {
		return Mname;
	}



	public void setMname(String mname) {
		Mname = mname;
	}



	public List<Jcs> getJcss() {
		return Jcss;
	}



	public void setJcss(List<Jcs> jcss) {
		Jcss = jcss;
	}



	public Jcs getJcs() {
		return jcs;
	}



	public void setJcs(Jcs jcs) {
		this.jcs = jcs;
	}



	public int getJcsid() {
		return Jcsid;
	}



	public void setJcsid(int jcsid) {
		Jcsid = jcsid;
	}



	public Zhsq getZhsq() {
		return zhsq;
	}



	public void setZhsq(Zhsq zhsq) {
		this.zhsq = zhsq;
	}
	
}
